package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.FoodCart;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.Streets;
import ex.com.jm.food.service.UserCartService;

public class UserCartAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
	

	private UserCartService userCartService;
	private String userId;
	private String cartId;
	private Integer num=1;
	private String menuId;
	private String cIds;//清空购物
	private Company company;
	private String comId;
	private List<Streets> streets=new ArrayList<Streets>();
	private String street;
	private String address;
	private Double profit=0.0;
	private List<FoodCart> cartOrders=new ArrayList<FoodCart>();
	public UserCartService getUserCartService() {
		return userCartService;
	}
	public void setUserCartService(UserCartService userCartService) {
		this.userCartService = userCartService;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getCartId() {
		return cartId;
	}
	public void setCartId(String cartId) {
		this.cartId = cartId;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public String getcIds() {
		return cIds;
	}
	public void setcIds(String cIds) {
		this.cIds = cIds;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public List<Streets> getStreets() {
		return streets;
	}
	public void setStreets(List<Streets> streets) {
		this.streets = streets;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public List<FoodCart> getCartOrders() {
		return cartOrders;
	}
	public void setCartOrders(List<FoodCart> cartOrders) {
		this.cartOrders = cartOrders;
	}
	public Double getProfit() {
		return profit;
	}
	public void setProfit(Double profit) {
		this.profit = profit;
	}
	/**
	 * 添加购物车
	 */
	public void addMenu(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		FoodCart fCart=this.getUserCartService().checkFoodCart(menuId, userId);//先查询此用户下是否有已添加这个菜品
		if(fCart!=null){
			//如果有的话，数量加+1
			this.getUserCartService().updateCart(fCart.getCartId(), fCart.getMenuNum()+1);
		}else{
			//若没有，就执行添加操作
			System.err.println(menuId);
			Menu menu=this.getUserCartService().getMenu(menuId);
			FoodCart foodCart = new FoodCart();
		    foodCart.setMenuId(menuId);
			foodCart.setMenuName(menu.getMenuName());
			foodCart.setMenuNum(num);
			foodCart.setMenuPrice(menu.getMenuNowPrice());
			foodCart.setMenuTotalPrice(num*menu.getMenuNowPrice());
			foodCart.setUserId(userId);
			this.getUserCartService().saveCart(foodCart);
		}
		Map map=new HashMap<String, String>();
		map.put("msg", "1");
		JSONObject jo=JSONObject.fromObject(map);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
		
	}
	/**
	 * 修改菜品数量
	 */
	public void updateMenu(){
		this.getUserCartService().updateCart(cartId, num);
	}
	/**
	 * 清空购物车
	 */
	public void delCart(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		String[] cid=new String[]{};
		cid=cIds.split(";");
		for (int i = 0; i < cid.length; i++) {
			this.getUserCartService().delCart(cid[i]);
		}
		Map map=new HashMap<String, String>();
		map.put("msg", "1");
		JSONObject jo=JSONObject.fromObject(map);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取购物车中所有的物品
	 */
	public void getCarts(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		List<FoodCart> foodCarts=this.getUserCartService().getCarts(userId);
		JSONArray jsonfc=JSONArray.fromObject(foodCarts);
		//JSONObject jo=JSONObject.fromObject(jsonfc);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jsonfc);
		out.print(jsonfc);
		out.flush();
		out.close();
		}
	/**
	 * 确认订单页面
	 * @return
	 */
	public String cartOrder(){
		company=this.getUserCartService().getCompany(comId);
		streets=this.getUserCartService().getStreets();
		cartOrders=this.getUserCartService().getCarts(userId);
		System.out.println(cartOrders.size());
		for (int i = 0; i < cartOrders.size(); i++) {
			num+=cartOrders.get(i).getMenuNum();
			profit+=cartOrders.get(i).getMenuTotalPrice();
		}
		num=num-1;
		return "cartOrderList";
	}
}
